package regEx.dfa;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import regEx.finiteAutomata.State;
import regEx.finiteAutomata.Transition;


public class DFAState extends State {

    /**
     * NFA states that make up the e-Closure for this DFAState
     */
    private Set<State> subsetStates;

    /** This is used to build the DFA from the NFA.  We mark the states after performing an e-Closure
     * and running a Move(->a) on each of its subsetStates.
     */
    private boolean marked;

    public DFAState(int id, Set<State> subsetStates) {
        super(id);
        this.marked = false;
        this.subsetStates = subsetStates;
    }

    public Set<State> getSubsetStates() {
        return subsetStates;
    }

    public void setSubsetStates(Set<State> subsetStates) {
        this.subsetStates = subsetStates;
    }

    /**
     * Overriding the compare function will allow use for collections
     * that use comparisons
     */
    public int compareTo(State otherState) {
        if (this.subsetStates.containsAll(((DFAState)otherState).getSubsetStates())) {
            return 0;
        } else {
            return 0;
        }
    }

    public boolean isMarked() {
        return marked;
    }

    public void setMarked(boolean marked) {
        this.marked = marked;
    }

    public ArrayList<State> move(char c) {
        ArrayList<State> moveStates = new ArrayList<State>();

        for(State s : this.getSubsetStates()) {
            for (Transition t : s.getTransitionsArray()) {
                if (t.getTransitionValue() == c) {
                    moveStates.add(t.getToState());
                }
            }
        }

        return moveStates;
    }

    @Override
    public boolean equals(Object obj) {
        return this.subsetStates.containsAll(((DFAState)obj).getSubsetStates());
    }
}
